  protected function appendLastProcessedItemToCart()
  {
    $ids = $this->configuration->getCartProcessedItems();
    if (count($ids) > 1)
    {
      $cart = $this->configuration->getCartItems();
      $cart[] = array_pop($ids);
      $cart[] = array_pop($ids);
      $this->configuration->setCartItems($cart);
      $this->configuration->setCartProcessedItems($ids);
      return $cart[count($cart)-1];
    }
    return null;
  }

  protected function popFromCart($condition = null)
  {
    $ids = $this->configuration->getCartItems();
    $object = null;
    while ($object == null && count($ids) > 0)
    {
      $object = <?php echo $this->getModelClass() ?>Peer::retrieveByPk(array_pop($ids));
      
      if (is_null($object) || '' == trim($condition))
      {
        $this->configuration->setCartItems($ids);
      }
      else
      {
        if ($object->$condition())
        {
          $this->configuration->setCartItems($ids);

          $processed = $this->configuration->getCartProcessedItems();
          if (!in_array($object->getPrimaryKey(), $processed)) $processed[] = $object->getPrimaryKey();
          $this->configuration->setCartProcessedItems($processed);
        }
        else
        {
          $object = null;
        }
      }
    }

    return $object;
  }

  public function executeCartFormActionBackwards(sfWebRequest $request)
  {
    $this->appendLastProcessedItemToCart();
    $this->forward('<?php echo $this->params['route_prefix'] ?>', 'cartFormAction');
  }

  public function executeCartFormAction(sfWebRequest $request)
  {
    $condition       = $request->getParameter('batch_c');
    $this->formClass = $request->getParameter('form_class', null);
    $this-><?php echo $this->getSingularName() ?> = $this->popFromCart($condition);

    if ($this->getUser()->hasFlash('error'))
    {
      $this->getUser()->setFlash('error', $this->getUser()->getFlash('error'));
    }

    if ($this->getUser()->hasFlash('notice'))
    {
      $this->getUser()->setFlash('notice', $this->getUser()->getFlash('notice'));
    }

    if (is_null($this-><?php echo $this->getSingularName() ?>))
    {
      $this->getUser()->setFlash('error', 'No eligible items in cart.');
      $this->redirect('<?php echo $this->params['route_prefix'] ?>/cartIndex');
    }

    $this->redirect('<?php echo $this->params['route_prefix'] ?>/cartEachFormAction?id='.$this-><?php echo $this->getSingularName() ?>->getId().'&form_class='.$this->formClass.'&batch_c='.$condition);
  }

  public function executeCartEachFormAction(sfWebRequest $request)
  {
    $condition = $request->getParameter('batch_c');
    $this-><?php echo $this->getSingularName() ?> = $this->getRoute()->getObject();

    $this->formClass = $request->getParameter('form_class');
    $this->form      = new $this->formClass($this-><?php echo $this->getSingularName() ?>);

    $this->cart_objects = $this->configuration->getCartItemsAsObjects($condition);
    $this->cart_processed_objects = $this->configuration->getCartProcessedItemsAsObjects();

    $this->setTemplate('cartFormAction');
  }

  public function executeCartEachFormActionUpdate(sfWebRequest $request)
  {
    $condition = $request->getParameter('batch_c');
    $this-><?php echo $this->getSingularName() ?> = <?php echo $this->getModelClass() ?>Peer::retrieveByPk($request->getParameter('id', -1));

    $this->formClass = $request->getParameter('form_class');
    $this->forward404If(is_null($this-><?php echo $this->getSingularName() ?>), '@<?php echo $this->getUrlForAction('list')?>');
    $this->form = new $this->formClass($this-><?php echo $this->getSingularName() ?>);

    $this->cartUpdateProcessForm($request, $this->form, $condition);

    $this->cart_objects = $this->configuration->getCartItemsAsObjects($condition);
    $this->cart_processed_objects = $this->configuration->getCartProcessedItemsAsObjects();

    $this->setTemplate('cartFormAction');
  }

  public function cartUpdateProcessForm($request, $form, $condition = null)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $<?php echo $this->getSingularName() ?> = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $<?php echo $this->getSingularName() ?>)));

      $remaining = $this->configuration->getCartItemsAsObjects($condition);
      if (count($remaining) <= 0)
      {
        $this->getUser()->setFlash('notice', 'The item was updated successfully. All items in cart have been edited.');
        $this->redirect('<?php echo $this->getModuleName()?>/cartIndex');
      }
      $this->getUser()->setFlash('notice', 'The item was updated successfully.');
      $this->redirect("<?php echo $this->params['route_prefix'] ?>/cartFormAction?form_class=".$request->getParameter('form_class').'&batch_c='.$condition);
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }

